import { http } from '@renderer/plugins/axios'
import { ref } from 'vue'
import useUtil from './useUtil'
import { ElMessage } from 'element-plus'

export default () => {
  const lossInfo = ref<LossModel>()
  const materialInfo = ref<MaterialModel>()
  const functionInfo = ref<FunctionInfoModel>()
  const templateList = ref<TemplateModel[]>()
  const computeInfo = ref<CalculateSetInfoModel>()
  const getLossRate = useUtil().request(async () => {
    try {
      const res = await http.request<LossModel>({
        url: '/getLossRate',
        method: 'GET',
      })
      delete res.id
      delete res.company_id
      lossInfo.value = res
    } catch (error) {
      console.log(error)
    }
  })
  const setLossRate = useUtil().request(async () => {
    try {
      await http.request({
        url: '/setLossRate',
        method: 'POST',
        data: lossInfo.value,
      })
      ElMessage({ type: 'success', message: '保存成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const getLossRateRecord = useUtil().request(async (params: any) => {
    try {
      const res = await http.request<LossModel>({
        url: '/getLossRateRecord',
        method: 'GET',
        params,
      })
      delete res.id
      delete res.company_id
      lossInfo.value = res
    } catch (error) {
      console.log(error)
    }
  })
  const setLossRateRecord = useUtil().request(async (data:any) => {
    try {
      await http.request({
        url: '/setLossRateRecord',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '保存成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const getMaterial = useUtil().request(async () => {
    try {
      const res = await http.request<MaterialModel>({
        url: '/getMaterial',
        method: 'GET',
      })
      delete res.id
      delete res.company_id
      materialInfo.value = res
    } catch (error) {
      console.log(error)
    }
  })
  const setMaterial = useUtil().request(async () => {
    try {
      await http.request({
        url: '/setMaterial',
        method: 'POST',
        data: materialInfo.value,
      })
      ElMessage({ type: 'success', message: '保存成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const getMaterialRecord = useUtil().request(async (params: any) => {
    try {
      const res = await http.request<MaterialModel>({
        url: '/getMaterialRecord',
        method: 'GET',
        params,
      })
      delete res.id
      delete res.company_id
      materialInfo.value = res
    } catch (error) {
      console.log(error)
    }
  })
  const setMaterialRecord = useUtil().request(async (data: any) => {
    try {
      await http.request({
        url: '/setMaterialRecord',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '保存成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const getCalculateTemplate = useUtil().request(async (params) => {
    try {
      templateList.value = []
      templateList.value = await http.request({
        url: '/getCalculateTemplate',
        method: 'GET',
        params,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const getCalculate = useUtil().request(async () => {
    try {
      functionInfo.value = await http.request<FunctionInfoModel>({
        url: '/getCalculate',
        method: 'GET',
      })
      console.log(functionInfo)
    } catch (error) {
      console.log(error)
    }
  })
  const setCalculate = useUtil().request(async (data: any) => {
    try {
      await http.request({
        url: '/setCalculate',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '保存成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const getCalculateRecord = useUtil().request(async (params: any) => {
    try {
      functionInfo.value = await http.request<FunctionInfoModel>({
        url: '/getCalculateRecord',
        method: 'GET',
        params,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const setCalculateRecord = useUtil().request(async (data: any) => {
    try {
      await http.request({
        url: '/setCalculateRecord',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '保存成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const getCalculateTableSet = useUtil().request(async (params: any) => {
    try {
      computeInfo.value = await http.request<CalculateSetInfoModel>({
        url: '/getCalculateTableSet',
        method: 'GET',
        params,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const calculateTableSet = useUtil().request(async (data: any) => {
    try {
      await http.request({
        url: '/calculateTableSet',
        method: 'POST',
        data,
      })
      ElMessage({ type: 'success', message: '保存成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const getLastGuiUpdate = useUtil().request(async (params: any) => {
    try {
      const res = await http.request({
        url: '/getLastGuiUpdate',
        method: 'GET',
        params,
      })
      return res
    } catch (error) {
      console.log(error)
      return null
    }
  })
  return {
    getLossRate,
    lossInfo,
    setLossRate,
    getLossRateRecord,
    setLossRateRecord,
    getMaterial,
    getMaterialRecord,
    materialInfo,
    setMaterial,
    setMaterialRecord,
    templateList,
    getCalculateTemplate,
    getCalculate,
    getCalculateRecord,
    functionInfo,
    setCalculate,
    setCalculateRecord,
    getCalculateTableSet,
    computeInfo,
    calculateTableSet,
    getLastGuiUpdate,
  }
}
